<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>猜数字练习</title>
</head>
<body>
    <h1>已生成一个1~100的随机数：</h1>
    <input type="text" id="i1" placeholder="请输入要猜的数字">
    <button onclick="guess()">点我验证</button>
    <h1>猜数字结果：<span class="result"></span></h1>
<script>
    let num1 = parseInt(Math.random()*100)+1;
    var count = 0;
    function guess(){
        let input = document.querySelector('#i1');
        console.log('作弊:'+ num1);

        let num2 = parseFloat(input.value);
        if(!/^\d$/.test(num2)){
            alert('请输入数字 ! ! !');
            return;
        }
        count++;
        let resultE = document.querySelector('.result');
        if(num1>num2){
            resultE.innerHTML = '猜小了';
        }else if(num1<num2){
            resultE.innerHTML = '猜大了';
        }else {
            resultE.innerHTML = '恭喜你用了'+count+'次猜对了';
        }


    }
</script>
</body>
</html>